<template>
  <h1>员工列表练习</h1>
  <el-form style="width: 700px;margin: 0 auto;">
    <el-form-item label="姓名">
      <el-input type="text" placeholder="请输入员工姓名" v-model="emp.name"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input type="text" placeholder="请输入员工工资" v-model="emp.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input type="text" placeholder="请输入员工岗位" v-model="emp.job"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="submit">点我提交</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="arr" style="width: 650px;margin: 0 auto">
    <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
    <el-table-column prop="name" label="姓名" align="center"></el-table-column>
    <el-table-column prop="salary" label="工资" align="center"></el-table-column>
    <el-table-column prop="job" label="岗位" align="center"></el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

//定义对象用来收集双向绑定的表单数据
const emp=ref({
  name:'',
  salary:'',
  job:''
});
//定义数组用来保存多个员工对象
const arr=ref([]);
//定义添加员工的方法
const submit=()=>{
  //对表单数据进行非空校验
  if(emp.value.name.trim()=='' || emp.value.salary.trim()=='' || emp.value.job.trim()==''){
    ElMessage.error('请输入员工信息！');
    return;
  }
  //将收集到的员工数据加入到数组中
  arr.value.push(emp.value);
  //清空输入框
  emp.value={name:'',salary:'',job:''}
}
//定义删除元素的方法
const del=(index,emp)=>{
  if(confirm('您确定要删除'+emp.name+'吗？')){
    arr.value.splice(index,1);
    ElMessage.success('删除成功！');
  }
}
</script>

<style scoped>

</style>